//
//  ProblemViewController.swift
//  GZMarathon
//
//  Created by apple on 2021/11/11.
//

import UIKit
import MJRefresh
class ProblemViewController: UIViewController {

    @IBOutlet weak var tab: UITableView!
    
    var dataSource: [GZFaqList] = []
    
    var flag = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "常见问题"
        tab.removeMoreLine()
        confTable()
    }
    
    func confTable(){
        tab.mj_header = MJRefreshNormalHeader(refreshingBlock: {[weak self] in
            self?.getFaqList()
        })
        tab.mj_header?.beginRefreshing()
    }
    
    func getFaqList(){
        ApiLoadingProvider.request(EBAPI.getFAQ, model: GZFaqData.self) {[weak self] model, isSuc in
            
            self?.tab.mj_header?.endRefreshing()
            guard let dt = model else{
                return
            }
            
            self?.dataSource = dt.data
            self?.tab.reloadData()
            
        }
    }

}

extension ProblemViewController:UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if(flag == 1){
            return 156
        }else{
            return 50
        }
        
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ProblemTableViewCell", for: indexPath) as! ProblemTableViewCell
        let faq = dataSource[indexPath.row]
        
        cell.conLabel.isHidden = false
        cell.numLabel.text = "\(indexPath.row + 1)"
        cell.conLabel.text = faq.faq_content
        cell.titleLabel.text = faq.faq_name
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        flag = 1
        tableView.reloadRows(at: [IndexPath(row: indexPath.row, section: indexPath.section)], with: .automatic)
        
    }
    
    
}
